<?php
class Tinbai_model extends CI_Model {
    var $tieude;
    var $tomtat;
    var $noidung;
    var $chuyenmuc;
    var $thumbnail;
    var $trangthai;
    var $ngonngu;
    function __construct()
    {
        parent::__construct();
    }
    //Xóa tin bài
    function xoa($id)
    {
        $this->db->where('id_tinbai',$id);
        $result=$this->db->delete('tinbai');
        return $result;
    }
    //Duyệt bài viết lên
    function duyetlen($id,$trangthai)
    {
        $this->db->where('id_tinbai',$id);
        $result=$this->db->update('tinbai',array('trangthai_tinbai'=>$trangthai));
        return $result;
    }
    //Xem tin bai theo chuyen muc $chuyenmuc="all" nếu muốn chọn tất
    function xemtinbai($id,$chuyenmuc)
    {
        if($chuyemuc="all")
            {
                $this->db->where('id_tinbai',$id);
                $result=$this->db->get('tinbai');
                return $result->result_array();
            }else 
                {
                    $this->db->where('id_tinbai',$id);
                    $this->db->where('chuyenmuc',$chuyenmuc);
                    $result=$this->db->get('tinbai');
                    return $result->result_array();
                }
    }
    //Sửa tinbai
    function suatinbai($id,$tieude,$tomtat,$noidung,$chuyenmuc,$thumbnail,$trangthai,$ngonngu,$nguoisua,$ten_nguoisua)
    {    	
        $data=array(
	        'tieude_tinbai'=>$tieude,
	        'tomtat_tinbai'=>$tomtat,
	        'noidung_tinbai'=>$noidung,
	        'chuyenmuc'=>$chuyenmuc,
	        'nguoidung_last'=>$nguoisua,
	        'ten_nguoidung_last'=>$ten_nguoisua,
	        'ngonngu'=>$ngonngu,
	        'trangthai_tinbai'=>$trangthai,
        	'datetime_last'=>date("Y-m-d H:i:s")
        );
         if($thumbnail != null) $data['thumbnail_tinbai'] = $thumbnail;
         $this->db->where('id_tinbai',$id);
         $result=$this->db->update('tinbai',$data);
         return $result;
         
    }
    //T?o tin bài
    function taotinbai($tieude,$tomtat,$noidung,$chuyenmuc,$thumbnail,$trangthai,$ngonngu,$id_marked,$nguoitao,$ten_nguoitao)
    {
        $data=array(
	        'id_mark'=>$id_marked,
	        'tieude_tinbai'=>$tieude,
	        'tomtat_tinbai'=>$tomtat,
	        'noidung_tinbai'=>$noidung,
	        'chuyenmuc'=>$chuyenmuc,
	        'thumbnail_tinbai'=>$thumbnail,
	        'nguoidung_first'=>$nguoitao,
	        'ten_nguoidung_first'=>$ten_nguoitao,
        	'ten_nguoidung_last'=>$ten_nguoitao,
        	'nguoidung_last'=>$nguoitao,
        	'datetime_first'=>date("Y-m-d H:i:s"),
        	'datetime_last'=>date("Y-m-d H:i:s"),
	        'ngonngu'=>$ngonngu,
	        'trangthai_tinbai'=>$trangthai
        );
         $result=$this->db->insert('tinbai',$data);
         return $this->db->insert_id();
    } 

    function takeUpNews($id,$status){
    	$data = array(
    		'trangthai_tinbai' => $status
    	);
    	$this->db->where('id_tinbai',$id);
    	return $this->db->update('tinbai',$data);
	}
	
	function takeEditNews($id){
    	$data = array(
    		'trangthai_tinbai' => 2
    	);
    	$this->db->where('id_tinbai',$id);
    	return $this->db->update('tinbai',$data);
	}
}
?>